final int UPPER_WINDOW_HEIGHT = 100; // to display raw data wave form

cdiff2 diff2;
cwaveform waveform;
cshowwindow showWindow;

// ========== setup ========== //
void setup() {
  println("this is a simple demo using diff^2 to find out if there is any desired pattern in a long waveform.\nI'm not sure it's a 100% correct algorithm.");
  println("but anyway the basic concept should be correct.");
  println("\npress 'a' or 's' to select 'abs' or 'square' mode");
  println("\nLeon Li, Jan 2025. V7\n");

  size(1800, 900);
  smooth();
  rectMode(CORNER);

  textSize(30);
  fill(#AAAAAA);

  // =========== //
  waveform = new cwaveform(rawData, pattern.length);

  // =========== //
  diff2 = new cdiff2(rawData, pattern);
  diff2.calculate("square");

  // =========== //
  showWindow = new cshowwindow(rawData, pattern);

  // =========== //
  stroke(#13A3BF, 80);
  strokeWeight(2);
}

// ========== draw ========== //
void draw() {
  background(#CCCCCC);
  fill(#EEEEEE);
  rect(0, 100, width, 200);

  // ===================== //
  diff2.show();
  waveform.show();

  if (mouseX > 2 && mouseX < width - 2) {
    showWindow.update(mouseX);
    showWindow.show();
  }

  // ===================== //
  line(mouseX, UPPER_WINDOW_HEIGHT, mouseX, height);
  fill(#BF7F08);
  text(diff2.getMode(), 1705, 870);
}

// ========== mouseClicked ========== //
void mouseClicked() {
  println(mouseX + " " + mouseY); // for debug
}

// ========== key press ========== //
void keyPressed() {
  if (key == 's') {
    diff2.calculate("square");
  } else if (key == 'a') {
    diff2.calculate("abs");
  }
}
